package sweet.helper

import java.io.{ File, FileReader, InputStream, FileInputStream, FileOutputStream }
import java.{ util=> jutil }
import scala.collection.{ mutable, immutable, jcl }

// =====================================================
// === scala to java AND java collection enhancement ===
// =====================================================

/** convert s.iterator to jcollection */
object JCollection{
  def apply[A](elems: scala.Iterator[A]) = new JCollection(elems)
  def apply[A](itr: scala.Iterable[A]) = new JCollection(itr)
}
class JCollection[A](elems: scala.Iterator[A]) extends jutil.AbstractCollection[A]{
  
  def this(itr: scala.Iterable[A]) = this(itr.elements)
  
  private lazy val elemsSize = elems.toList.size
  def iterator = new jutil.Iterator[A]{
    def hasNext = elems.hasNext
    def next = elems.next
    def remove = throw new UnsupportedOperationException("remove")
  }
  def size = elemsSize  
}

/** convert s.seq to jlist */
object JList{
  //def apply[A](seq: scala.Seq[A]) = new JList(seq)
  def apply[A](tuples: (A)*) = new JList(tuples)
}
class JList[A](seq: scala.Seq[A]) extends jutil.AbstractList[A] {
  private lazy val seqSize = seq.size
  def get(idx: Int) = seq(idx)
  def size = seqSize
}

/** java map enhancement */
object JMap{
  def apply[A, B](pairs: (A,B)*): JMap[A,B] = {
    val map = new JMap[A,B]
    for((k,v) <- pairs) map.put(k,v)
    map
  }
  
  def apply[A, B](imap: scala.collection.Map[A,B]): JMap[A,B] = {
    val m = new JMap[A,B]
    for((k,v) <- imap) m.put(k,v)
    m 
  }
}
class JMap[A,B] extends jutil.HashMap[A,B]

/** java properties enhancement */
object JProperties{
  def apply(): jutil.Properties = new jutil.Properties  
  def apply(name: String): jutil.Properties = apply(new FileInputStream(name))
  def apply(file: File): jutil.Properties = apply(new FileInputStream(file))
  def apply(ins: InputStream): jutil.Properties = {
    val props = new jutil.Properties
    props.load(ins)
    props
  }
  
  def loadFromXML(file: File): jutil.Properties = {
    val props = JProperties()
    props.loadFromXML(new FileInputStream(file))
    props
  } 
}

/*
class JMap[A](map: scala.collection.Map[A, B]) extends jutil.AbstractMap[A, B] {
  def entrySet: jutilSet = {
    new JCollection(map
  }
}*/

// =====================================================
// === java to scala AND java collection enhancement ===
// =====================================================
object SProperties {
  def apply() : SProperties = new SProperties(JProperties())
  def apply(filename: String): SProperties = new SProperties(JProperties(filename))
  def apply(file: File): SProperties = new SProperties(JProperties(file))
  def apply(ins: InputStream): SProperties = new SProperties(JProperties(ins))
  
  def loadFromXML(file: File): SProperties = new SProperties(JProperties.loadFromXML(file))
}
class SProperties(props: jutil.Properties)
  extends jcl.Hashtable[String,String](props.asInstanceOf[jutil.Hashtable[String,String]]){
  def storeToXML(file: File, comment: String, enc: String){
    props.storeToXML(new FileOutputStream(file), comment, enc)  
  }
}

class SStringMap(map: jutil.Map[String,String]) extends jcl.MapWrapper[String,String]{
  def underlying = map
}

